package com.jack.hiltsimple.mvvm.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.jack.hiltsimple.mvvm.model.BannerRepository
import com.jack.hiltsimple.third_sdk.ApiResponse
import com.jack.hiltsimple.third_sdk.BanInfos
import dagger.hilt.android.lifecycle.HiltViewModel
import jakarta.inject.Inject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * @创建者 Jack
 * @创建时间 2025-08-21 16:20
 * @描述
 */
// BannerViewModel.kt
@HiltViewModel // 关键注解
class BannerViewModel @Inject constructor(
    private val bannerRepository: BannerRepository // 通过构造函数注入 Repository
) : ViewModel() {

    var banner = MutableLiveData<MutableList<BanInfos>>()

    fun loadBanner() {
        val fetchedBanner = bannerRepository.getBanner()
        fetchedBanner.enqueue(object : Callback<ApiResponse<MutableList<BanInfos>>> {
            override fun onResponse(
                call: Call<ApiResponse<MutableList<BanInfos>>>,
                response: Response<ApiResponse<MutableList<BanInfos>>>
            ) {
                val result: MutableList<BanInfos>? = response.body()?.data
                banner.value = result!!
            }

            override fun onFailure(
                call: Call<ApiResponse<MutableList<BanInfos>>>,
                t: Throwable
            ) {
            }
        })
    }
}